<?php

namespace app\admin\controller;

use think\facade\Request;
use think\Validate;
use app\common\model\UserModel;
use app\common\model\GameModel;
use app\common\model\GameFruitFlowModel;
use app\common\model\GameDiceFlowModel;

class GameController extends BaseController
{

    public function index()
    {
        return $this->fetch();
    }

    public function index_handle()
    {
        $where = [];
        if ($name = Request::param('name') != '') {
            $where[] = ['name', 'like', "%{$name}%"];
        }
        $list = GameModel::where($where)->order("id", "desc")->select();

        if (count($list) > 0) {
            return json(["code" => 0, "msg" => "", "data" => $list, "count" => count($list)]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    public function add()
    {
        return $this->fetch();
    }

    public function add_handle()
    {
        $params = Request::param();
        $validate    = new Validate([
            'name'  => 'require|length:1,50',
            'thumb'  => 'require|length:1,500',
            'url'  => 'require',
        ], [
            "name"  => "游戏名称在1-50个字符",
            "thumb"  => "请上传游戏Logo",
            "url" => "请填写游戏链接"
        ]);

        if (!$validate->check($params)) {
            return self::bulidFail($validate->getError());
        }

        $new = new GameModel($params);

        if ($new->save()) {
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function edit()
    {
        $id = Request::param("id");
        $ret = GameModel::get($id);
        $this->assign("ret", $ret);
        return $this->fetch();
    }

    public function edit_handle()
    {
        $id = Request::param("id");
        if ($id <= 0) {
            return self::bulidFail();
        }
        $params = Request::param();
        $validate    = new Validate([
            'name'  => 'require|length:1,50',
            'thumb'  => 'require|length:1,500',
            'url'  => 'require',
        ], [
            "name"  => "游戏名称在1-50个字符",
            "thumb"  => "请上传游戏Logo",
            "url" => "请填写游戏链接"
        ]);

        if (!$validate->check($params)) {
            return self::bulidFail($validate->getError());
        }

        $ret = GameModel::get($id);
        $ret->name = $params['name'];
        $ret->name_en = $params['name_en'];
        $ret->thumb = $params['thumb'];
        $ret->url = $params['url'];
        $ret->sort = $params['sort'];

        if ($ret->save()) {
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function delete()
    {
        $id = Request::param("id");
        $ret = GameModel::where("id", $id)->delete();
        if ($ret) {
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function fruit_flow()
    {
        return $this->fetch();
    }

    public function fruit_flow_handle()
    {
        $where = [];
        $uid =  Request::param('user_id');
        $type = Request::param('type');
        $st =  Request::param('start_time');
        $et = Request::param('end_time');
        if ($uid  != '') {
            $where[] = ['user_id', '=', $uid];
        }
        if ($type  != '') {
            $where[] = ['award_type', '=', $type];
        }
        if ($st != '') {
            $where[] = ['ctime', '>=', strtotime($st)];
        }
        if ($et != '') {
            $where[] = ['ctime', '<=', strtotime($et)];
        }

        $count = GameFruitFlowModel::where($where)->count();

        $list = GameFruitFlowModel::where($where)->page($this->get('page', 1), $this->get('limit', 20))->order("id", "desc")->select();

        foreach ($list as $item) {
            $item['time'] = date("Y-m-d H:i:s", $item['ctime']);
            $item['nickname'] = UserModel::get($item['user_id'])['nick_name'];
            $item['type'] = GameFruitFlowModel::$types[$item['award_type']];
            $item['bet'] = 0;
            $bets = json_decode($item['bet_data'], true);
            foreach ($bets as $b) {
                $item['bet'] += $b['amount'];
            }
        }

        if (count($list) > 0) {
            return json(["code" => 0, "msg" => "", "data" => $list, "count" => $count]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    public function dice_flow()
    {
        return $this->fetch();
    }

    public function dice_flow_handle()
    {
        $where = [];
        $uid =  Request::param('user_id');
        $st =  Request::param('start_time');
        $et = Request::param('end_time');
        $is_reward =  Request::param('is_reward');
        if ($uid  != '') {
            $where[] = ['user_id', '=', $uid];
        }
        if ($st != '') {
            $where[] = ['ctime', '>=', strtotime($st)];
        }
        if ($et != '') {
            $where[] = ['ctime', '<=', strtotime($et)];
        }
        if ($is_reward != '') {
            $where[] = ['is_reward', '=', $is_reward];
        }

        $totals = GameDiceFlowModel::where($where)
        ->field([
            'COUNT(*) as count',
            'SUM(bet_num) as total_bet_num',
            'SUM(award_amount) as total_award_amount'
        ])
        ->find();

        $list = GameDiceFlowModel::where($where)->page($this->get('page', 1), $this->get('limit', 20))->order("id", "desc")->select();

        foreach ($list as $item) {
            $item['time'] = date("Y-m-d H:i:s", $item['ctime']);
            $item['nickname'] = UserModel::get($item['user_id'])['nick_name'];
        }

        if (count($list) > 0) {
            return json([
                "code" => 0,
                "msg" => "",
                "data" => $list,
                "count" => $totals['count'] ?? 0,
                "total_bet_num" => $totals['total_bet_num'] ?? 0,
                "total_award_amount" => $totals['total_award_amount'] ?? 0
            ]);
        }
        return self::bulidFail("未查询到相关数据");
    }
}
